﻿using System;
using System.Linq;
using System.Linq.Expressions;
using System.Linq.Dynamic;
using System.Text;
using System.Data.Linq;
using System.Transactions;
using LinqToSqlExtensions;
using System.ServiceModel;
using System.Collections;
using System.Collections.Generic;
using CardsServer.DAL;

namespace CardsServer.WCF
{
    public partial interface IService
    {
        [OperationContract]
        string FK_Add(FK model, int ACID);


        [OperationContract]
        string FK_Update(FK model, int ACID);


        [OperationContract]
        string FKBypid_Delete(string pid, int ACID);


        [OperationContract]
        FK FKBypid_Get(string pid, int ACID);


        [OperationContract]
        List<FK> FK_Get(string sortBy, int ACID);




    }
    public partial class Service : IService
    {
        #region  添加FK表数据
        /// <summary>
        /// 添加FK
        /// </summary>
        /// <param name="model">FK实体</param>
        /// <returns>空或错误</returns>
        public string FK_Add(FK model, int ACID)
        {
            if (string.IsNullOrEmpty(model.pid))
            {
                return "『单据号』不能为空！";
            }
            if (model.zrje == null)
            {
                return "『折让金额』不能为空！";
            }
            if (model.sfje == null)
            {
                return "『实付金额』不能为空！";
            }
            if (model.yfje == null)
            {
                return "『应付金额』不能为空！";
            }
            if (model.opid == null || model.opid.Equals(Guid.Empty))
            {
                return "『制单人ID』不能为空！";
            }
            if (model.empid == null || model.empid.Equals(Guid.Empty))
            {
                return "『经手人ID』不能为空！";
            }
            if (model.ClientID == null || model.ClientID.Equals(Guid.Empty))
            {
                return "『客户ID』不能为空！";
            }
            if (model.Pdate == null)
            {
                return "『开单日期』不能为空！";
            }
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                try
                {
                    if (ctx.FK.Count(p => p.pid == model.pid) > 0)
                    {
                        return "主键已存在！";
                    }
                    ctx.FK.InsertOnSubmit(model);
                    ctx.SubmitChanges();
                    return string.Empty;
                }
                catch (Exception ex)
                {
                    return " 添加失败!错误详细信息：" + ex.Message;
                }
            }
        }
        #endregion

        #region  添加FK表数据事物用重构
        /// <summary>
        /// 添加FK事物用重构
        /// </summary>
        /// <param name="model">FK实体</param>
        /// <returns>空或错误</returns>
        private string FK_Add(FK model, int ACID, CardsServer.DAL.DataClassesDataContext ctx)
        {
            if (string.IsNullOrEmpty(model.pid))
            {
                return "『单据号』不能为空！";
            }
            //if (model.zrje == null)
            //{
            //    return "『折让金额』不能为空！";
            //}
            if (model.sfje == null)
            {
                return "『实付金额』不能为空！";
            }
            if (model.yfje == null)
            {
                return "『应付金额』不能为空！";
            }
            if (model.opid == null || model.opid.Equals(Guid.Empty))
            {
                return "『制单人ID』不能为空！";
            }
            if (model.empid == null || model.empid.Equals(Guid.Empty))
            {
                return "『经手人ID』不能为空！";
            }
            if (model.ClientID == null || model.ClientID.Equals(Guid.Empty))
            {
                return "『客户ID』不能为空！";
            }
            if (model.Pdate == null)
            {
                return "『开单日期』不能为空！";
            }
            if (ctx.FK.Count(p => p.pid == model.pid) > 0)
            {
                return "主键已存在！";
            }
            ctx.FK.InsertOnSubmit(model);
            return string.Empty;
        }
        #endregion



        #region  修改FK表数据
        /// <summary>
        /// 修改FK
        /// </summary>
        /// <param name="model">FK实体</param>
        /// <returns>空或错误</returns>
        public string FK_Update(FK model, int ACID)
        {
            if (string.IsNullOrEmpty(model.pid))
            {
                return "『单据号』不能为空！";
            }
            if (model.zrje == null)
            {
                return "『折让金额』不能为空！";
            }
            if (model.sfje == null)
            {
                return "『实付金额』不能为空！";
            }
            if (model.yfje == null)
            {
                return "『应付金额』不能为空！";
            }
            if (model.opid == null || model.opid.Equals(Guid.Empty))
            {
                return "『制单人ID』不能为空！";
            }
            if (model.empid == null || model.empid.Equals(Guid.Empty))
            {
                return "『经手人ID』不能为空！";
            }
            if (model.ClientID == null || model.ClientID.Equals(Guid.Empty))
            {
                return "『客户ID』不能为空！";
            }
            if (model.Pdate == null)
            {
                return "『开单日期』不能为空！";
            }
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                try
                {
                    if (ctx.FK.Count(p => p.pid == model.pid) != 1)
                    {
                        return "未找到指定记录或记录不唯一！";
                    }
                    FK FKmodel = ctx.FK.Single(p => p.pid == model.pid);
                    if (!string.IsNullOrEmpty(model.pid))
                    {
                        FKmodel.pid = model.pid;
                    }
                    if (model.Pdate != null)
                    {
                        FKmodel.Pdate = model.Pdate;
                    }
                    if (model.ClientID != null || model.ClientID.Equals(Guid.Empty))
                    {
                        FKmodel.ClientID = model.ClientID;
                    }
                    if (model.empid != null || model.empid.Equals(Guid.Empty))
                    {
                        FKmodel.empid = model.empid;
                    }
                    if (model.opid != null || model.opid.Equals(Guid.Empty))
                    {
                        FKmodel.opid = model.opid;
                    }
                    if (model.yfje != null)
                    {
                        FKmodel.yfje = model.yfje;
                    }
                    if (model.sfje != null)
                    {
                        FKmodel.sfje = model.sfje;
                    }
                    if (model.zrje != null)
                    {
                        FKmodel.zrje = model.zrje;
                    }
                    if (!string.IsNullOrEmpty(model.remarks))
                    {
                        FKmodel.remarks = model.remarks;
                    }
                    if (!string.IsNullOrEmpty(model.Intro))
                    {
                        FKmodel.Intro = model.Intro;
                    }
                    ctx.SubmitChanges();
                    return string.Empty;
                }
                catch (Exception ex)
                {
                    return " 修改失败!错误详细信息：" + ex.Message;
                }
            }
        }
        #endregion



        #region  删除FK表Bypid数据
        /// <summary>
        /// 删除FKBypid
        /// </summary>
        /// <param name="pid">单据号</param>
        /// <returns>空或错误</returns>
        public string FKBypid_Delete(string pid, int ACID)
        {
            if (string.IsNullOrEmpty(pid))
            {
                return "『单据号』不能为空！";
            }
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                try
                {
                    if (ctx.FK.Count(p => p.pid == pid) != 1)
                    {
                        return null;
                    }
                    FK model = ctx.FK.Single(p => p.pid == pid);
                    ctx.FK.DeleteOnSubmit(model);
                    ctx.SubmitChanges();
                    return string.Empty;
                }
                catch (Exception ex)
                {
                    return " 删除失败!错误详细信息：" + ex.Message;
                }
            }
        }
        #endregion

        #region  删除FK表Bypid数据[事物用重构]
        /// <summary>
        /// 删除FKBypid[事物用重构]
        /// </summary>
        /// <param name="pid">单据号</param>
        /// <returns>空或错误</returns>
        private string FKBypid_Delete(string pid, int ACID, CardsServer.DAL.DataClassesDataContext ctx)
        {
            if (string.IsNullOrEmpty(pid))
            {
                return "『单据号』不能为空！";
            }
            if (ctx.FK.Count(p => p.pid == pid) != 1)
            {
                return null;
            }
            FK model = ctx.FK.Single(p => p.pid == pid);
            ctx.FK.DeleteOnSubmit(model);
            return string.Empty;
        }
        #endregion



        #region  获取FK表Bypid数据
        /// <summary>
        /// 获取FKBypid
        /// </summary>
        /// <param name="pid">单据号</param>
        /// <returns>当前表</returns>
        public FK FKBypid_Get(string pid, int ACID)
        {
            if (string.IsNullOrEmpty(pid))
            {
                return null;
            }
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                try
                {
                    if (ctx.FK.Count(p => p.pid == pid) != 1)
                    {
                        return null;
                    }
                    return ctx.FK.SingleOrDefault(p => p.pid == pid);
                }
                catch (Exception ex)
                {
                    return null;
                }
            }
        }
        #endregion



        #region  获取FK表数据
        /// <summary>
        /// 获取FK
        /// </summary>
        /// <param name="sortBy">排序属性名</param>
        /// <param name="sortType">排序类型0=升序 1=降序</param>
        /// <returns>List<FK></returns>
        public List<FK> FK_Get(Expression<Func<FK, bool>> predicate, string sortBy, int ACID)
        {
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                try
                {
                    if (predicate == null)
                    {
                        if (string.IsNullOrWhiteSpace(sortBy))
                        {
                            return ctx.FK.ToList();
                        }
                        else
                        {
                            return ctx.FK.OrderBy(sortBy).ToList();
                        }
                    }
                    else
                    {
                        if (string.IsNullOrWhiteSpace(sortBy))
                        {
                            return ctx.FK.Where(predicate).ToList();
                        }
                        else
                        {
                            return ctx.FK.Where(predicate).OrderBy(sortBy).ToList();
                        }
                    }
                }
                catch (Exception ex)
                {
                    return null;
                }
            }
        }
        #endregion


        #region  获取FK表数据
        /// <summary>
        /// 获取FK
        /// </summary>
        /// <param name="sortBy">排序属性名</param>
        /// <param name="sortType">排序类型0=升序 1=降序</param>
        /// <returns>List<FK></returns>
        public List<FK> FK_Get(string sortBy, int ACID)
        {
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                try
                {
                    return FK_Get(null, sortBy, ACID);
                }
                catch (Exception)
                {
                    return null;
                }
            }
        }
        #endregion


        #region  获取FK表数据计数
        /// <summary>
        /// 获取FK表数据计数
        /// </summary>
        /// <returns>List<FK></returns>
        public int FK_GetCount(Expression<Func<FK, bool>> predicate, int ACID)
        {
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                try
                {
                    if (predicate == null)
                    {
                        return ctx.FK.Count();

                    }
                    else
                    {
                        return ctx.FK.Where(predicate).Count();
                    }
                }
                catch
                {
                    return 0;
                }
            }
        }
        #endregion




        #region  获取FK分页表数据
        /// <summary>
        /// 获取FK分页表数据
        /// </summary>
        /// <param name="pageIndex">当前页</param>
        /// <param name="pageSize">页大小</param>
        /// <param name="sortBy">排序属性名</param>
        /// <param name="sortType">排序类型0=升序 1=降序</param>
        /// <returns>List<FK></returns>
        public List<FK> FK_Getpage(int pageIndex, int pageSize, Expression<Func<FK, bool>> predicate, string sortBy, int ACID)
        {
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                try
                {
                    if (predicate == null)
                    {
                        if (string.IsNullOrWhiteSpace(sortBy))
                        {
                            return ctx.FK.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
                        }
                        else
                        {
                            return ctx.FK.OrderBy(sortBy).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
                        }
                    }
                    else
                    {
                        if (string.IsNullOrWhiteSpace(sortBy))
                        {
                            return ctx.FK.Where(predicate).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
                        }
                        else
                        {
                            return ctx.FK.Where(predicate).OrderBy(sortBy).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
                        }
                    }
                }
                catch (Exception ex)
                {
                    return null;
                }
            }
        }
        #endregion


        #region  批量修改FK表数据
        /// <summary>
        /// 批量修改FK
        /// </summary>
        /// <param name="predicate">Expression<Func<FK, bool>></param> 查找表达式树
        /// <param name="updater">Expression<Func<FK,FK>> 更新表达式树</param>
        /// <param name="returns">影响记录数</param>
        public int FK_batchUpdate(Expression<Func<FK, bool>> predicate, Expression<Func<FK, FK>> updater, int ACID)
        {
            using (TransactionScope ts = new TransactionScope())
            {
                using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
                {
                    try
                    {
                        int ok = ctx.FK.Update(predicate, updater);
                        ts.Complete();
                        return ok;
                    }
                    catch (Exception ex)
                    {
                        return 0;
                    }
                }
            }
        }
        #endregion


        #region  批量修改FK表数据事物用重构
        /// <summary>
        /// 批量修改FK事物用重构
        /// </summary>
        /// <param name="predicate">Expression<Func<FK, bool>></param> 查找表达式树
        /// <param name="updater">Expression<Func<FK,FK>> 更新表达式树</param>
        /// <param name="ctx">数据上下文</param>
        /// <param name="returns">影响记录数</param>
        private int FK_batchUpdate(Expression<Func<FK, bool>> predicate, Expression<Func<FK, FK>> updater, int ACID, CardsServer.DAL.DataClassesDataContext ctx)
        {
            int ok = ctx.FK.Update(predicate, updater);
            return ok;
        }
        #endregion


        #region  批量删除FK表数据
        /// <summary>
        /// 批量删除FK
        /// </summary>
        /// <param name="predicate">Expression<Func<FK, bool>></param> 查找表达式树
        /// <param name="returns">影响记录数</param>
        public int FK_batchDel(Expression<Func<FK, bool>> predicate, int ACID)
        {
            using (TransactionScope ts = new TransactionScope())
            {
                using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
                {
                    try
                    {
                        int ok = ctx.FK.Delete(predicate);
                        ts.Complete();
                        return ok;
                    }
                    catch (Exception ex)
                    {
                        return 0;
                    }
                }
            }
        }
        #endregion


        #region  批量删除FK表数据事物用重构
        /// <summary>
        /// 批量删除FK事物用重构
        /// </summary>
        /// <param name="predicate">Expression<Func<FK, bool>></param> 查找表达式树
        /// <param name="ctx">数据上下文</param>
        /// <param name="returns">影响记录数</param>
        private int FK_batchDel(Expression<Func<FK, bool>> predicate, int ACID, CardsServer.DAL.DataClassesDataContext ctx)
        {
            int ok = ctx.FK.Delete(predicate);
            return ok;
        }
        #endregion


    }
}

